package com.guiji.quartz.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.domain.CCTVSociety;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-14 15:24
 **/
public class DelPicUtil {
	private static final Logger log = LoggerFactory.getLogger(DelPicUtil.class);

	public static List<String> delPic(List<Object> imgList) {
		List<String> newImg = new ArrayList<>();
		for (Object img : imgList) {
			try {
				String name = SecureUtil.md5(img.toString()) + ".jpg";
				String picPath = "/data/cms/dist/proxypic/" + name;
				boolean exist = FileUtil.exist(picPath);
				if(!exist){
					HttpResponse response = HttpRequest.get(img.toString()).setHttpProxy(ProxyConfig.url, ProxyConfig.ip).setConnectionTimeout(15000)
						.execute();
					long l = response.writeBody(new File(picPath));
					//执行scp将图片拷贝到55节点
					String shell = "scp "+picPath+" root@172.16.160.55:/data/hdd/douyin/proxypic/";
					Process exec = Runtime.getRuntime().exec(shell, null, null);
					boolean tag = exec.waitFor(10, TimeUnit.SECONDS);
					if(l>10000&&tag==true){
						newImg.add("http://172.16.160.55/proxypic/" + name);
					}else {
						log.error("scp图片错误{}",DateUtil.now());
						continue;
					}
				}
			} catch (Exception e) {
				continue;
			}
		}
		return newImg;
	}

	public static List<String> delPic1(List<Object> imgList) {
		List<String> newImg = new ArrayList<>();
		for (Object img : imgList) {
			if (!img.toString().contains("res")) {
				continue;
			}
			newImg.add(img.toString().replaceAll("\\.\\./\\.\\./\\.\\./", "https://www.rarb.cn/"));
		}
		return newImg;
	}

	public static List<String> delPic2(List<Object> imgList, String pre) {
		List<String> newImg = new ArrayList<>();
		for (Object img : imgList) {
			newImg.add(img.toString().replaceAll("\\.\\./\\.\\./\\.\\./", pre));
		}
		return newImg;
	}

	public static List<String> delPic3(List<Object> imgList) {
		List<String> newImg = new ArrayList<>();
		for (Object img : imgList) {
			newImg.add("http://en.people.cn"+img.toString());
		}
		return newImg;
	}

//	public static void main(String[] args) {
//		boolean exist = FileUtil.exist("C:\\Users\\Administrator\\Desktop\\aaa.jpg");
//		System.out.println(exist);
//		HttpResponse response = HttpRequest.get("http://172.16.6.72/proxypic/690c4a0f25ae8177018f5ad6f24c2430.jpg").setConnectionTimeout(15000)
//			.execute();
//		long l = response.writeBody(new File("C:\\Users\\Administrator\\Desktop\\aaa.jpg"));
//		System.out.println(l);
//	}
}
